<?php

namespace app\api\controller;

use app\api\validate\user\UserFollowValidate;
use app\common\controller\Frontend;
use app\common\enum\user\UserFollowTypeEnum;
use app\logic\user\FollowLogic;
use app\logic\user\UserLogic;
use think\facade\Log;

class Follow extends Frontend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();

    }

    /**
     * 我的关注
     */
    public function myFollow()
    {
        $limit = (int)$this->request->param('size');
        if ($limit > 15 || $limit < 0) {
            $limit = 15;
        }

        try {
            $onlySelectCreator = true;
            if ($this->auth->isCreator()) {
                $onlySelectCreator = false;
            }
            $result = FollowLogic::getInstance()->myFollow($this->auth->id, $limit, $onlySelectCreator);
        }catch (\Throwable $exception) {
            $message = config('app.env') != 'prod' ? $exception->getMessage().PHP_EOL . $exception->getTraceAsString() : __('server error');
            Log::channel('my')->error($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());
            $this->error($message);
        }

        $this->success(__('success'), $result);
    }

    /**
     * 我的粉丝
     */
    public function myFans()
    {
        $limit = (int)$this->request->param('size');
        if ($limit > 15 || $limit < 0) {
            $limit = 15;
        }

        //检查当前登录者是否是达人
        if (!$this->auth->isCreator()) {
            $this->error(__('403 Forbidden'), null, 403, null, ['statusCode' => 403]);
        }

        try {
            $result = FollowLogic::getInstance()->myFans($this->auth->id, $limit);
        }catch (\Throwable $exception) {
            $message = config('app.env') != 'prod' ? $exception->getMessage().PHP_EOL . $exception->getTraceAsString() : __('server error');
            Log::channel('my')->error($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());
            $this->error($message);
        }

        $this->success(__('success'), $result);
    }

    /**
     * 关注用户/达人
     */
    public function follow()
    {
        // 获取POST请求参数
        $params = $this->request->param(['user_id', 'type']);

        // 实例化验证器对象，用于后续的参数验证
        $validate = new UserFollowValidate();

        // 使用验证器的创建场景进行参数验证，如果验证失败则抛出异常
        try {
            $validate->scene('follow')->check($params);
        } catch (\Throwable $e) {
            // 如果验证失败，显示错误消息
            $this->error($e->getMessage());
        }

        try {
            UserLogic::getInstance()->follow($this->auth->getUserInfo(), (string)$params['user_id'], $params['type']);
        } catch (\RuntimeException $e) {
            $this->error($e->getMessage());
        } catch (\Throwable $exception) {
            $this->error(__('Server error'));
        }

        if (UserFollowTypeEnum::from($params['type']) === UserFollowTypeEnum::CANCEL) {
            $this->success(__('Unfollow'));
        }
        if (UserFollowTypeEnum::from($params['type']) === UserFollowTypeEnum::FOLLOW) {
            $this->success(__('Follow success'));
        }
    }
}